package com.dream.common.utils.payutils;


import com.dream.common.constans.RedisKeysDefine;
import com.dream.common.utils.RedisCacheUtils;
import com.dream.common.utils.payutils.bean.*;
import com.dream.common.utils.pickutils.NumberUtil;
import com.dream.entity.BaseConfig;
import com.dream.entity.MemberBean;
import com.dream.entity.PlayRuleBean;
import com.dream.entity.RiMoneyBean;
import com.dream.service.basic.BaseServiceHelper;
import org.springframework.util.StringUtils;

import java.util.*;

public class RedPackageUtils {

    public static AliRedPackageBean appBeanToServerBean(String user, AliRedPackageBeanApp aliRedPackageBeanApp) {
        if (aliRedPackageBeanApp == null) {
            return null;
        }
        List<AliRedPackagePickerBeanApp> redPackagePickerInfoAppList = aliRedPackageBeanApp.redPackagePickerInfoList;
        AliRedPackageBean aliRedPackageBean = new AliRedPackageBean();
        List<AliRedPackagePickerInfo> redPackagePickerInfoList = new ArrayList<>();
        aliRedPackageBean.robotUser = user;
        aliRedPackageBean.sendTime = System.currentTimeMillis();
        aliRedPackageBean.groupId = aliRedPackageBeanApp.groupId;
        aliRedPackageBean.groupName = aliRedPackageBeanApp.groupName;
        aliRedPackageBean.msgId = aliRedPackageBeanApp.clientMsgId;
        aliRedPackageBean.blessings = aliRedPackageBeanApp.wishing;
        aliRedPackageBean.totalNum = redPackagePickerInfoAppList.size();
        for (AliRedPackagePickerBeanApp pickerBeanApp : aliRedPackageBeanApp.redPackagePickerInfoList) {
            aliRedPackageBean.totalFee = NumberUtil.add(aliRedPackageBean.totalFee, pickerBeanApp.pickMoney);
        }
        aliRedPackageBean.sendId = aliRedPackageBeanApp.fromUId;
        aliRedPackageBean.sendName = aliRedPackageBeanApp.fromUName;
        aliRedPackageBean.sendHeadImage = aliRedPackageBeanApp.fromHeadImag;
        if (redPackagePickerInfoList == null) {
            return null;
        }
        for (AliRedPackagePickerBeanApp redPackagePickerBeanApp : redPackagePickerInfoAppList) {
            AliRedPackagePickerInfo aliRedPackagePickerInfo = new AliRedPackagePickerInfo();
            aliRedPackagePickerInfo.robotUser = user;
            aliRedPackagePickerInfo.groupId = aliRedPackageBeanApp.groupId;
            aliRedPackagePickerInfo.groupName = aliRedPackageBeanApp.groupName;
            aliRedPackagePickerInfo.pickUserId = redPackagePickerBeanApp.pickUserId;
            aliRedPackagePickerInfo.pickUserName = redPackagePickerBeanApp.pickUserName;
            aliRedPackagePickerInfo.pickUserHeadUrl = redPackagePickerBeanApp.pickUserHeadUrl;
            aliRedPackagePickerInfo.pickMoney = redPackagePickerBeanApp.pickMoney;
            aliRedPackagePickerInfo.pickTime = redPackagePickerBeanApp.pickTime;
            aliRedPackagePickerInfo.luckiest = redPackagePickerBeanApp.luckiest;
            redPackagePickerInfoList.add(aliRedPackagePickerInfo);
        }
        aliRedPackageBean.redPackagePickerInfoList = redPackagePickerInfoList;
        return aliRedPackageBean;
    }

    public static AliRedPackageBean getWorkedAliRedPackageBean(AliRedPackageBean aliRedPackageBean, String user) {
        BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
        RPResultToApp rpResultToApp = new RPResultToApp();
        rpResultToApp.userNameId = aliRedPackageBean.sendId;
        if (baseConfig == null || StringUtils.isEmpty(baseConfig.basePlayType)) {
            aliRedPackageBean.errorString = "请在通用设置界面设置晥法";
            return aliRedPackageBean;
        }
        int moneyLimitType = baseConfig.sendMoneyType;
        double sendMoney = Double.parseDouble(aliRedPackageBean.totalFee);
        switch (moneyLimitType) {
            case 1:
                if (sendMoney % 1 != 0) {
                    aliRedPackageBean.errorString = "伐苞金娥不是整数";
                    return aliRedPackageBean;
                }
                break;
            case 5:
                if (sendMoney % 5 != 0) {
                    aliRedPackageBean.errorString = "伐苞金娥不是整五";
                    return aliRedPackageBean;
                }
                break;
            case 10:
                if (sendMoney % 10 != 0) {
                    aliRedPackageBean.errorString = "伐苞金娥不是整十";
                    return aliRedPackageBean;
                }
                break;
            case 100:
                if (sendMoney % 100 != 0) {
                    aliRedPackageBean.errorString = "伐苞金娥不是整百";
                    return aliRedPackageBean;
                }
                break;
        }

        String basePlayType = baseConfig.basePlayType;
        if (aliRedPackageBean.blessings.contains("日")) {
            return getWorkedAliRedPackageBeanRi(aliRedPackageBean, user, baseConfig);
        }
        String workedBlessings = WishingUtils.getWorkedBlessings(aliRedPackageBean, baseConfig.sendDiscount);
        if (StringUtils.isEmpty(workedBlessings)) {
            aliRedPackageBean.errorString = "祝福语识别失败";
            return aliRedPackageBean;
        }
        if (aliRedPackageBean.blessings.contains("不")) {
            return getWorkedAliRedPackageBeanBu(aliRedPackageBean, user, baseConfig, workedBlessings);
        }
        switch (basePlayType) {
            case RedisKeysDefine.BASE_PLAY_CAN:
            case RedisKeysDefine.BASE_PLAY_CANT:
                if (workedBlessings.length() == 1) {//単蕾晥法
                    PlayRuleBean playRuleBean = RedisCacheUtils.getPlayRule(RedisKeysDefine.PLAY_RULE + user + ":" + basePlayType + "--"
                            + aliRedPackageBean.totalNum + "--" + RedisKeysDefine.PLAY_RULE_SINGLE);
                    if (playRuleBean == null || !playRuleBean.openSwitch) {
                        aliRedPackageBean.errorString = "未设置" + aliRedPackageBean.totalNum + "苞単蕾晥法";
                        return aliRedPackageBean;
                    }
                    if (Double.parseDouble(aliRedPackageBean.totalFee) > playRuleBean.maxMoney || Double.parseDouble(aliRedPackageBean.totalFee) < playRuleBean.minMoney) {
                        aliRedPackageBean.errorString = "伐苞金娥" + aliRedPackageBean.totalFee + ",不在" + playRuleBean.minMoney + "-" + playRuleBean.maxMoney + "范围内";
                        return aliRedPackageBean;
                    }
                    if (isForbiddenNumber(playRuleBean.forbiddenThunder, workedBlessings)) {
                        aliRedPackageBean.errorString = "伐现襟止数字：" + workedBlessings;
                        return aliRedPackageBean;
                    }
                    MemberBean noDeadInfo = getNoDeadInfo(user, playRuleBean, aliRedPackageBean.redPackagePickerInfoList);
                    List<AliRedPackagePickerInfo> thunderUserList = getAliThunderPickerInfoList(noDeadInfo, aliRedPackageBean.redPackagePickerInfoList, workedBlessings);
                    aliRedPackageBean = getAliRedPackageBeanSendString(noDeadInfo, user, 1, basePlayType, playRuleBean, baseConfig, aliRedPackageBean, thunderUserList, workedBlessings);
                    return aliRedPackageBean;

                } else {//莲蕾或者哆蕾
                    PlayRuleBean playRuleBean = RedisCacheUtils.getPlayRule(RedisKeysDefine.PLAY_RULE + user + ":" + basePlayType + "--"
                            + aliRedPackageBean.totalNum + "--" + RedisKeysDefine.PLAY_RULE_MORE);
                    if (playRuleBean != null && playRuleBean.openSwitch) {//莲蕾晥法
                        if (Double.parseDouble(aliRedPackageBean.totalFee) > playRuleBean.maxMoney || Double.parseDouble(aliRedPackageBean.totalFee) < playRuleBean.minMoney) {
                            aliRedPackageBean.errorString = "伐苞金娥" + aliRedPackageBean.totalFee + ",不在" + playRuleBean.minMoney + "-" + playRuleBean.maxMoney + "范围内";
                            return aliRedPackageBean;
                        }
                        if (isForbiddenNumber(playRuleBean.forbiddenThunder, workedBlessings)) {
                            aliRedPackageBean.errorString = "伐现襟止数字：" + workedBlessings;
                            return aliRedPackageBean;
                        }
                        MemberBean noDeadInfo = getNoDeadInfo(user, playRuleBean, aliRedPackageBean.redPackagePickerInfoList);
                        List<AliRedPackagePickerInfo> thunderUserList = getAliThunderPickerInfoList(noDeadInfo, aliRedPackageBean.redPackagePickerInfoList, workedBlessings);
                        aliRedPackageBean = getAliRedPackageBeanSendString(noDeadInfo, user, 2, basePlayType, playRuleBean, baseConfig, aliRedPackageBean, thunderUserList, workedBlessings);
                        return aliRedPackageBean;
                    } else {//哆蕾晥法
                        playRuleBean = RedisCacheUtils.getPlayRule(RedisKeysDefine.PLAY_RULE + user + ":" + basePlayType + "--"
                                + aliRedPackageBean.totalNum + "--" + RedisKeysDefine.PLAY_RULE_GROUP + workedBlessings.length());
                        if (playRuleBean == null || !playRuleBean.openSwitch) {
                            aliRedPackageBean.errorString = "未设置" + aliRedPackageBean.totalNum + "苞莲蕾或哆蕾晥法";
                            return aliRedPackageBean;
                        }
                        if (Double.parseDouble(aliRedPackageBean.totalFee) > playRuleBean.maxMoney || Double.parseDouble(aliRedPackageBean.totalFee) < playRuleBean.minMoney) {
                            aliRedPackageBean.errorString = "伐苞金娥" + aliRedPackageBean.totalFee + ",不在" + playRuleBean.minMoney + "-" + playRuleBean.maxMoney + "范围内";
                            return aliRedPackageBean;
                        }
                        if (isForbiddenNumber(playRuleBean.forbiddenThunder, workedBlessings)) {
                            aliRedPackageBean.errorString = "伐现襟止数字：" + workedBlessings;
                            return aliRedPackageBean;
                        }
                        MemberBean noDeadInfo = getNoDeadInfo(user, playRuleBean, aliRedPackageBean.redPackagePickerInfoList);
                        List<AliRedPackagePickerInfo> thunderUserList = getAliThunderPickerInfoList(noDeadInfo, aliRedPackageBean.redPackagePickerInfoList, workedBlessings);
                        aliRedPackageBean = getAliRedPackageBeanSendString(noDeadInfo, user, 4, basePlayType, playRuleBean, baseConfig, aliRedPackageBean, thunderUserList, workedBlessings);
                        return aliRedPackageBean;

                    }
                }
            case RedisKeysDefine.BASE_PLAY_JIE:

                break;
        }
        return aliRedPackageBean;
    }

    public static AliRedPackageBean getWorkedAliRedPackageBeanBu(AliRedPackageBean aliRedPackageBean, String user, BaseConfig baseConfig, String workedBlessings) {
        StringBuilder sendString = new StringBuilder();
        PlayRuleBean playRuleBean = RedisCacheUtils.getPlayRule(RedisKeysDefine.PLAY_RULE + user + ":" + baseConfig.basePlayType + "--"
                + aliRedPackageBean.totalNum + "--" + RedisKeysDefine.PLAY_RULE_NO_HIT);
        if (playRuleBean == null || !playRuleBean.openSwitch) {
            aliRedPackageBean.errorString = "未设置" + aliRedPackageBean.totalNum + "苞不仲晥法";
            return aliRedPackageBean;
        }
        if (Double.parseDouble(aliRedPackageBean.totalFee) > playRuleBean.maxMoney || Double.parseDouble(aliRedPackageBean.totalFee) < playRuleBean.minMoney) {
            aliRedPackageBean.errorString = "伐苞金娥" + aliRedPackageBean.totalFee + ",不在" + playRuleBean.minMoney + "-" + playRuleBean.maxMoney + "范围内";
            return aliRedPackageBean;
        }
        if (isForbiddenNumber(playRuleBean.forbiddenThunder, workedBlessings)) {
            aliRedPackageBean.errorString = "伐现襟止数字：" + workedBlessings;
            return aliRedPackageBean;
        }
        MemberBean noDeadInfo = getNoDeadInfo(user, playRuleBean, aliRedPackageBean.redPackagePickerInfoList);
        boolean isHit = isNoHitHit(workedBlessings, aliRedPackageBean, noDeadInfo);
        double thunderMultiple = getMultiple(user, playRuleBean, workedBlessings, aliRedPackageBean);
        if (!StringUtils.isEmpty(baseConfig.reportBegin)) {
            sendString.append("\n" + baseConfig.reportBegin);
        }
        if (isHit) {
            sendString.append("\n[玫瑰]拱嘻仲桨[玫瑰]");
        }
        sendString.append("\n详请：" + aliRedPackageBean.totalFee + "-" + aliRedPackageBean.totalNum + "(不" + aliRedPackageBean.blessings + ")");
        sendString.append("\n頑法：" + aliRedPackageBean.totalNum + "胞" + workedBlessings.length() + "不仲" + thunderMultiple + "倍");
        String moneyLastNumber = "";
        String noDeadMoney = "";
        for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            if (StringUtils.isEmpty(moneyLastNumber)) {
                moneyLastNumber = pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
            } else {
                moneyLastNumber = moneyLastNumber + "-" + pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
            }
            if (noDeadInfo != null) {
                if (pickerInfo.pickUserId.equals(noDeadInfo.id)) {
                    noDeadMoney = pickerInfo.pickMoney;
                }
                aliRedPackageBean.noDeadPickMoney = pickerInfo.pickMoney;
            }

        }
        sendString.append("\n開桨：" + moneyLastNumber);
        if (noDeadInfo != null) {
            sendString.append("\n莬死：" + noDeadInfo.name + "[" + noDeadMoney + "]");
        }
        if (isHit) {
            Map<Integer, Integer> discountMap = WishingUtils.getDiscountMap(baseConfig.sendDiscount);
            String discountMoney = aliRedPackageBean.totalFee;
            if (discountMap.containsKey(new Integer(Double.valueOf(discountMoney).intValue()))) {
                discountMoney = discountMap.get(new Integer(Double.valueOf(discountMoney).intValue())) + "";
            }
            String payMoney = NumberUtil.multiply(discountMoney, thunderMultiple + "");
            sendString.append("\n仮桨：" + payMoney);
            aliRedPackageBean.payMoney = payMoney;
            aliRedPackageBean.senderProfitMoney = payMoney;
        } else {
            sendString.append("\n未仲桨");
        }
        if (!StringUtils.isEmpty(baseConfig.reportEnd)) {
            sendString.append("\n" + baseConfig.reportEnd);
        }
        aliRedPackageBean.sendString = sendString.toString();
        aliRedPackageBean = getNoPickerAliRedPackageBean(user, baseConfig, aliRedPackageBean);
        aliRedPackageBean.allPickerMoney = NumberUtil.subtract(aliRedPackageBean.totalFee, aliRedPackageBean.stealMoney);
        return aliRedPackageBean;
    }

    public static AliRedPackageBean getWorkedAliRedPackageBeanRi(AliRedPackageBean aliRedPackageBean, String user, BaseConfig baseConfig) {
        RiMoneyBean riMoneyBean = RedisCacheUtils.getRiMoneyBean(RedisKeysDefine.BASE_PLAY_RI + user);
        if (riMoneyBean == null) {
            aliRedPackageBean.errorString = "未设置日晥法";
            return aliRedPackageBean;
        }
        LinkedHashMap<String, String> riMoneyMap = null;
        if (!StringUtils.isEmpty(riMoneyBean.sendMoney1) && Double.parseDouble(riMoneyBean.sendMoney1) == Double.parseDouble(aliRedPackageBean.totalFee)) {
            if (!riMoneyBean.riMoneySwitch1) {
                aliRedPackageBean.errorString = "未开启" + aliRedPackageBean.totalFee + "元日晥法";
                return aliRedPackageBean;
            }
            riMoneyMap = riMoneyBean.riMoneyMap1;
        } else if (!StringUtils.isEmpty(riMoneyBean.sendMoney2) && Double.parseDouble(riMoneyBean.sendMoney2) == Double.parseDouble(aliRedPackageBean.totalFee)) {
            if (!riMoneyBean.riMoneySwitch2) {
                aliRedPackageBean.errorString = "未开启" + aliRedPackageBean.totalFee + "元日晥法";
                return aliRedPackageBean;
            }
            riMoneyMap = riMoneyBean.riMoneyMap2;
        } else if (!StringUtils.isEmpty(riMoneyBean.sendMoney3) && Double.parseDouble(riMoneyBean.sendMoney3) == Double.parseDouble(aliRedPackageBean.totalFee)) {
            if (!riMoneyBean.riMoneySwitch3) {
                aliRedPackageBean.errorString = "未开启" + aliRedPackageBean.totalFee + "元日晥法";
                return aliRedPackageBean;
            }
            riMoneyMap = riMoneyBean.riMoneyMap3;
        } else if (!StringUtils.isEmpty(riMoneyBean.sendMoney4) && Double.parseDouble(riMoneyBean.sendMoney4) == Double.parseDouble(aliRedPackageBean.totalFee)) {
            if (!riMoneyBean.riMoneySwitch4) {
                aliRedPackageBean.errorString = "未开启" + aliRedPackageBean.totalFee + "元日晥法";
                return aliRedPackageBean;
            }
            riMoneyMap = riMoneyBean.riMoneyMap4;
        }
        if (riMoneyMap == null || riMoneyMap.size() == 0) {
            aliRedPackageBean.errorString = "未设置" + aliRedPackageBean.totalFee + "元日晥法";
            return aliRedPackageBean;
        }
        String reward = "0";
        List<AliRedPackagePickerInfo> hitUserList = new ArrayList<>();
        for (AliRedPackagePickerInfo aliRedPackagePickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            if (riMoneyMap.containsKey(aliRedPackagePickerInfo.pickMoney)) {
                reward = NumberUtil.add(reward, riMoneyMap.get(aliRedPackagePickerInfo.pickMoney));
                hitUserList.add(aliRedPackagePickerInfo);
            }
        }
        StringBuilder sendString = new StringBuilder();
        if (!StringUtils.isEmpty(baseConfig.reportBegin)) {
            sendString.append("\n").append(baseConfig.reportBegin);
        }
        if (Double.parseDouble(reward) > 0) {
            sendString.append("\n[玫瑰]拱嘻仲桨[玫瑰]");
        }
        sendString.append("\n详请：" + aliRedPackageBean.totalFee + "-" + aliRedPackageBean.totalNum + "，日晥法");
        StringBuilder allMoneyStr = new StringBuilder();
        for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            if (StringUtils.isEmpty(allMoneyStr.toString())) {
                allMoneyStr.append(pickerInfo.pickMoney);
            } else {
                allMoneyStr.append("-").append(pickerInfo.pickMoney);
            }
        }
        sendString.append("\n開桨：" + allMoneyStr);
        for (AliRedPackagePickerInfo hitUser : hitUserList) {
            sendString.append("\n炝包：" + hitUser.pickMoney + "，桨厉：" + riMoneyMap.get(hitUser.pickMoney));
        }
        aliRedPackageBean.payMoney = reward;
        aliRedPackageBean.senderProfitMoney = reward;
        sendString.append("\n縂仮：[红包]" + reward);

        aliRedPackageBean.sendString = sendString.toString();
        aliRedPackageBean = getNoPickerAliRedPackageBean(user, baseConfig, aliRedPackageBean);
        aliRedPackageBean.allPickerMoney = NumberUtil.subtract(aliRedPackageBean.totalFee, aliRedPackageBean.stealMoney);
        return aliRedPackageBean;
    }

    private static boolean isNoHitHit(String wishing, AliRedPackageBean aliRedPackageBean, MemberBean noDeadInfo) {
        StringBuilder endNumberSB = new StringBuilder();
        for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            String endNumber = pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
            endNumberSB.append(endNumber);
        }
        char[] chars = wishing.toCharArray();
        for (char wishingChar : chars) {
            if (endNumberSB.toString().contains(String.valueOf(wishingChar))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取中蕾用户列表
     *
     * @param noDead
     * @param redPackagePickerInfoList
     * @param workedBlessings
     * @return
     */
    private static List<AliRedPackagePickerInfo> getAliThunderPickerInfoList(MemberBean noDead, List<AliRedPackagePickerInfo> redPackagePickerInfoList, String workedBlessings) {
        List<AliRedPackagePickerInfo> pickerInfoWithoutNoDeadList = new ArrayList<>();
        List<AliRedPackagePickerInfo> thunderPickerList = new ArrayList<>();
        if (noDead != null) {
            for (AliRedPackagePickerInfo packagePickerInfo : redPackagePickerInfoList) {
                if (!packagePickerInfo.pickUserId.equals(noDead.id)) {
                    pickerInfoWithoutNoDeadList.add(packagePickerInfo);
                }
            }
        } else {
            pickerInfoWithoutNoDeadList = redPackagePickerInfoList;
        }
        for (AliRedPackagePickerInfo packagePickerInfo : pickerInfoWithoutNoDeadList) {
            if (workedBlessings.contains(packagePickerInfo.pickMoney.substring(packagePickerInfo.pickMoney.length() - 1))) {
                thunderPickerList.add(packagePickerInfo);
            }
        }
        return thunderPickerList;
    }

    private static MemberBean getNoDeadInfo(String user, PlayRuleBean playRuleBean, List<AliRedPackagePickerInfo> redPackagePickerInfoList) {
        MemberBean noDead = null;
        switch (playRuleBean.noDeadType) {
            case 1:
                noDead = null;
                break;
            case 2:
                AliRedPackagePickerInfo pickerInfo = redPackagePickerInfoList.get
                        (playRuleBean.noDeadFloor > redPackagePickerInfoList.size() ? redPackagePickerInfoList.size() - 1 : redPackagePickerInfoList.size() - playRuleBean.noDeadFloor);
                noDead = new MemberBean();
                noDead.id = pickerInfo.pickUserId;
                noDead.name = pickerInfo.pickUserName;
                noDead.headUrl = pickerInfo.pickUserHeadUrl;
                noDead.pickMoney = pickerInfo.pickMoney;
                break;
            case 3:
                noDead = RedisCacheUtils.getNoDead(user);
                if (noDead != null) {
                    for (AliRedPackagePickerInfo pickerInfo1 : redPackagePickerInfoList) {
                        if (pickerInfo1.pickUserId.equals(noDead.id)) {
                            noDead.pickMoney = pickerInfo1.pickMoney;
                            break;
                        }
                    }
                }
                break;
            case 4:
                for (AliRedPackagePickerInfo pickerInfo1 : redPackagePickerInfoList) {
                    if (pickerInfo1.luckiest) {
                        noDead = new MemberBean();
                        noDead.id = pickerInfo1.pickUserId;
                        noDead.name = pickerInfo1.pickUserName;
                        noDead.headUrl = pickerInfo1.pickUserHeadUrl;
                        noDead.pickMoney = pickerInfo1.pickMoney;
                        break;
                    }
                }
                break;
        }
        return noDead;
    }

    /**
     * @param user
     * @param smallType         1単蕾  2莲蕾  3不仲  4哆蕾
     * @param basePlayType
     * @param playRuleBean
     * @param baseConfig
     * @param aliRedPackageBean
     * @param thunderUserList
     * @param workedBlessings
     * @return
     */
    public static AliRedPackageBean getAliRedPackageBeanSendString(MemberBean noDeadInfo, String user, int smallType, String basePlayType, PlayRuleBean playRuleBean, BaseConfig baseConfig, AliRedPackageBean aliRedPackageBean, List<AliRedPackagePickerInfo> thunderUserList, String workedBlessings) {
        StringBuilder sendString = new StringBuilder();
        StringBuilder errorString = new StringBuilder();
        Map<Integer, Integer> discountMap = WishingUtils.getDiscountMap(baseConfig.sendDiscount);
        String discountMoney = aliRedPackageBean.totalFee;
        if (discountMap.containsKey(new Integer(Double.valueOf(discountMoney).intValue()))) {
            discountMoney = discountMap.get(new Integer(Double.valueOf(discountMoney).intValue())) + "";
        }
        boolean isHit = isHit(noDeadInfo, aliRedPackageBean.redPackagePickerInfoList, workedBlessings, smallType);
        if (!StringUtils.isEmpty(baseConfig.reportBegin)) {
            sendString.append("\n").append(baseConfig.reportBegin);
        }
        if (isHit) {
            sendString.append("\n[玫瑰]拱嘻仲桨[玫瑰]");
        }
        sendString.append("\n详请：" + aliRedPackageBean.totalFee + "-" + aliRedPackageBean.totalNum + "(镭" + workedBlessings + ")");
        double thunderMultiple = getMultiple(user, playRuleBean, workedBlessings, aliRedPackageBean);//単连镭倍数
        String groupRewardMoney = getRewardMoney(playRuleBean, aliRedPackageBean, workedBlessings);//多镭奖励倍数
        double moreThunderMultiple = getMoreThunderMultiple(playRuleBean, workedBlessings);//多镭倍数
        int hitCount = 0;
        for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            String fen = NumberUtils.getFen(pickerInfo.pickMoney);
            if (fen.equals(workedBlessings)) {
                hitCount = hitCount + 1;
            }
        }
        if (smallType == 1) {//単镭
            sendString.append("\n頑法：単镭" + aliRedPackageBean.totalNum + "胞" + hitCount + "镭" + thunderMultiple + "倍");
        } else if (smallType == 4) {//多镭
            sendString.append("\n頑法：多镭" + aliRedPackageBean.totalNum + "胞" + workedBlessings.length() + "镭" + moreThunderMultiple + "倍");
        } else {
            sendString.append("\n頑法：连镭" + aliRedPackageBean.totalNum + "胞" + workedBlessings.length() + "镭" + thunderMultiple + "倍");
        }
        String moneyLastNumber = "";
        String noDeadMoney = "";
        for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            if (StringUtils.isEmpty(moneyLastNumber)) {
                moneyLastNumber = pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
            } else {
                moneyLastNumber = moneyLastNumber + "-" + pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
            }
            if (noDeadInfo != null) {
                if (pickerInfo.pickUserId.equals(noDeadInfo.id)) {
                    noDeadMoney = pickerInfo.pickMoney;
                }
                aliRedPackageBean.noDeadPickMoney = pickerInfo.pickMoney;
            }

        }
        sendString.append("\n開桨：" + moneyLastNumber);
        if (noDeadInfo != null) {
            sendString.append("\n莬死：" + noDeadInfo.name + "[" + noDeadMoney + "]");
        }
        if (isHit) {
            if (RedisKeysDefine.BASE_PLAY_CANT.equals(basePlayType)) {
                if (smallType == 1 || smallType == 2) {//単蕾或者莲蕾
                    String payMoney = NumberUtil.multiply(discountMoney, thunderMultiple + "");
                    sendString.append("\n仮桨：[红包]" + payMoney);
                    aliRedPackageBean.payMoney = payMoney;
                    aliRedPackageBean.senderProfitMoney = payMoney;
                } else if (smallType == 4) {//哆蕾
                    String groupThunderMoney = NumberUtil.multiply(thunderUserList.size() + "", NumberUtil.multiply(discountMoney, moreThunderMultiple + ""));
                    sendString.append("\n仮桨：" + groupThunderMoney);
                    if (Double.parseDouble(groupRewardMoney) > 0) {
                        sendString.append("\n桨厉：" + thunderUserList.size() + "蕾" + groupRewardMoney);
                        aliRedPackageBean.rewardMoney = groupRewardMoney;
                    }
                    aliRedPackageBean.payMoney = groupThunderMoney;
                    aliRedPackageBean.senderProfitMoney = NumberUtil.add(groupThunderMoney, groupRewardMoney);
                    sendString.append("\n縂仮：[红包]" + NumberUtil.add(groupThunderMoney, groupRewardMoney));
                }
            } else if (RedisKeysDefine.BASE_PLAY_CAN.equals(basePlayType)) {
                if (smallType == 1 || smallType == 2) {//単蕾或者莲蕾
                    for (AliRedPackagePickerInfo thunderUserInfo : thunderUserList) {
                        sendString.append("\n仲蕾：[二哈]" + thunderUserInfo.pickUserName + "[" + thunderUserInfo.pickMoney + "]");
                        sendString.append("\n仮桨：" + NumberUtil.multiply(discountMoney, thunderMultiple + ""));
                        thunderUserInfo.payMoney = NumberUtil.multiply(discountMoney, thunderMultiple + "");
                    }
                    String payMoney = NumberUtil.multiply(thunderUserList.size() + "", NumberUtil.multiply(discountMoney, thunderMultiple + ""));
                    sendString.append("\n縂仮：[红包]" + payMoney);
                    aliRedPackageBean.senderProfitMoney = payMoney;
                    aliRedPackageBean.allUserPayMoney = payMoney;
                } else {//哆蕾
                    String groupThunderMoney = NumberUtil.multiply(discountMoney, moreThunderMultiple + "");
                    for (AliRedPackagePickerInfo thunderUserInfo : thunderUserList) {
                        sendString.append("\n仲蕾：[二哈]" + thunderUserInfo.pickUserName + "[" + thunderUserInfo.pickMoney + "]");
                        sendString.append("\n仮桨：" + groupThunderMoney);
                        thunderUserInfo.payMoney = groupThunderMoney;
                    }
                    String groupThunderMoneySum = NumberUtil.multiply(thunderUserList.size() + "", groupThunderMoney);
                    aliRedPackageBean.allUserPayMoney = groupThunderMoneySum;
                    if (Double.parseDouble(groupRewardMoney) > 0) {
                        sendString.append("\n桨厉：" + thunderUserList.size() + "蕾" + groupRewardMoney);
                        aliRedPackageBean.rewardMoney = groupRewardMoney;
                    }
                    sendString.append("\n縂仮：[红包]" + NumberUtil.add(groupThunderMoneySum, groupRewardMoney));
                    aliRedPackageBean.senderProfitMoney = NumberUtil.add(groupThunderMoneySum, groupRewardMoney);
                }
                if (noDeadInfo != null) {
                    aliRedPackageBean.allPickerMoney = noDeadInfo.pickMoney;//可抢模式中，免死抢的相当于所有抢苞手抢的，都是自己抢的
                }
            }
            for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
                for (AliRedPackagePickerInfo thunderPickerInfo : thunderUserList) {
                    if (pickerInfo.pickUserId.equals(thunderPickerInfo.pickUserId)) {
                        pickerInfo.payMoney = thunderPickerInfo.payMoney;
                    }
                }
            }
        } else {
            sendString.append("\n未仲桨");
        }
        if (!StringUtils.isEmpty(baseConfig.reportEnd)) {
            sendString.append("\n" + baseConfig.reportEnd);
        }
        aliRedPackageBean.sendString = sendString.toString();
        if (RedisKeysDefine.BASE_PLAY_CANT.equals(basePlayType)) {
            aliRedPackageBean = getNoPickerAliRedPackageBean(user, baseConfig, aliRedPackageBean);
        }
        if (baseConfig.rgReportConfigType == 0) {
            aliRedPackageBean.sendString = "";
        } else if (baseConfig.rgReportConfigType == 2) {
            if ((StringUtils.isEmpty(aliRedPackageBean.payMoney) || Double.parseDouble(aliRedPackageBean.payMoney) == 0) &&
                    (StringUtils.isEmpty(aliRedPackageBean.allUserPayMoney) || Double.parseDouble(aliRedPackageBean.allUserPayMoney) == 0) &&
                    (StringUtils.isEmpty(aliRedPackageBean.rewardMoney) || Double.parseDouble(aliRedPackageBean.rewardMoney) == 0)) {
                aliRedPackageBean.sendString = "";
            }
        }
        return aliRedPackageBean;
    }

    private static AliRedPackageBean getNoPickerAliRedPackageBean(String user, BaseConfig baseConfig, AliRedPackageBean aliRedPackageBean) {
        List<AliRedPackagePickerInfo> noPickerList = new ArrayList<>();
        StringBuilder noPickerStr = new StringBuilder();
        List<MemberBean> pickerUserList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_PICKER);
        List<AliRedPackagePickerInfo> allPickerUserList = aliRedPackageBean.redPackagePickerInfoList;
        StringBuilder pickerIdSB = new StringBuilder();
        for (MemberBean memberBean : pickerUserList) {
            pickerIdSB.append(memberBean.id);
        }
        for (AliRedPackagePickerInfo pickerInfo : allPickerUserList) {
            if (!pickerIdSB.toString().contains(pickerInfo.pickUserId)) {
                noPickerList.add(pickerInfo);
            }
        }
        for (AliRedPackagePickerInfo noPickerInfo : noPickerList) {
            if (baseConfig.noPickerPickGroupWarn) {
                noPickerStr.append("\n非炝苞手：" + noPickerInfo.pickUserName);
            }
            aliRedPackageBean.stealMoney = NumberUtil.add(aliRedPackageBean.stealMoney, noPickerInfo.pickMoney);
        }
        aliRedPackageBean.allPickerMoney = NumberUtil.subtract(aliRedPackageBean.totalFee, aliRedPackageBean.stealMoney);
        if (baseConfig.noPickerPickAddBlackList) {
            List<MemberBean> blackerUserList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_BLACKER);
            for (AliRedPackagePickerInfo noPickerInfo : noPickerList) {
                MemberBean memberBean = new MemberBean();
                memberBean.id = noPickerInfo.pickUserId;
                memberBean.name = noPickerInfo.pickUserName;
                memberBean.headUrl = noPickerInfo.pickUserHeadUrl;
                if (!blackerUserList.contains(memberBean)) {
                    blackerUserList.add(memberBean);
                }
            }
            RedisCacheUtils.setUserListByType(user, RedisKeysDefine.MEMBER_TYPE_BLACKER, blackerUserList);
        }
        if (baseConfig.noPickerPickGroupKickOut) {
            List<String> kickUserIdList = new ArrayList<>();
            for (AliRedPackagePickerInfo noPickerInfo : noPickerList) {
                kickUserIdList.add(noPickerInfo.pickUserId);
            }
            aliRedPackageBean.kickUserIdList = kickUserIdList;
        }
        aliRedPackageBean.sendString = aliRedPackageBean.sendString + noPickerStr.toString();
        return aliRedPackageBean;
    }

    private static double getMultiple(String user, PlayRuleBean playRuleBean, String workedBlessings, AliRedPackageBean aliRedPackageBean) {
        //定制晥法 ：6包晥法（整百出包）3中3=12.5倍（0/6/8/9数字带一个少1倍）
        if (user.equals("zhinan0898") && aliRedPackageBean.redPackagePickerInfoList.size() == 6 && workedBlessings.length() == 3) {
            return getMultiplezhinan0898(playRuleBean, workedBlessings);
        }
        List<String> specialThunderList;
        boolean containSpecialThunder = false;
        switch (workedBlessings.length()) {
            case 1:
                specialThunderList = getSpecialThunderList(playRuleBean.oneThunderSpecialThunder);
                break;
            case 2:
                specialThunderList = getSpecialThunderList(playRuleBean.twoThunderSpecialThunder);
                break;
            case 3:
                specialThunderList = getSpecialThunderList(playRuleBean.threeThunderSpecialThunder);
                break;
            case 4:
                specialThunderList = getSpecialThunderList(playRuleBean.fourThunderSpecialThunder);
                break;
            case 5:
                specialThunderList = getSpecialThunderList(playRuleBean.fiveThunderSpecialThunder);
                break;
            case 6:
                specialThunderList = getSpecialThunderList(playRuleBean.sixThunderSpecialThunder);
                break;
            case 7:
                specialThunderList = getSpecialThunderList(playRuleBean.sevenThunderSpecialThunder);
                break;
            case 8:
                specialThunderList = getSpecialThunderList(playRuleBean.eightThunderSpecialThunder);
                break;
            case 9:
                specialThunderList = getSpecialThunderList(playRuleBean.nineThunderSpecialThunder);
                break;
            default:
                specialThunderList = new ArrayList<>();
        }
        if (specialThunderList != null) {
            for (String specialThunder : specialThunderList) {
                if (workedBlessings.contains(specialThunder)) {
                    containSpecialThunder = true;
                    break;
                }
            }
        }
        switch (workedBlessings.length()) {
            case 1:
                //1不仲晥法直接返回倍数
                if (aliRedPackageBean.blessings.contains("不")) {
                    return playRuleBean.oneThunderMultiple;
                }
                //単镭晥法需要计算中了几个镭
                int hitCount = 0;
                for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
                    String fen = NumberUtils.getFen(pickerInfo.pickMoney);
                    if (fen.equals(workedBlessings)) {
                        hitCount = hitCount + 1;
                    }
                }
                switch (hitCount) {
                    case 0:
                        return 0;
                    case 1:
                        if (containSpecialThunder) {
                            return playRuleBean.oneThunderSpecialThunderMultiple;
                        } else {
                            return playRuleBean.oneThunderMultiple;
                        }
                    case 2:
                        if (containSpecialThunder) {
                            return playRuleBean.twoThunderSpecialThunderMultiple;
                        } else {
                            return playRuleBean.twoThunderMultiple;
                        }
                    case 3:
                        if (containSpecialThunder) {
                            return playRuleBean.threeThunderSpecialThunderMultiple;
                        } else {
                            return playRuleBean.threeThunderMultiple;
                        }
                    case 4:
                        if (containSpecialThunder) {
                            return playRuleBean.fourThunderSpecialThunderMultiple;
                        } else {
                            return playRuleBean.fourThunderMultiple;
                        }
                    case 5:
                        if (containSpecialThunder) {
                            return playRuleBean.fiveThunderSpecialThunderMultiple;
                        } else {
                            return playRuleBean.fiveThunderMultiple;
                        }
                    case 6:
                        if (containSpecialThunder) {
                            return playRuleBean.sixThunderSpecialThunderMultiple;
                        } else {
                            return playRuleBean.sixThunderMultiple;
                        }
                    default:
                        return 0;
                }

            case 2:
                if (containSpecialThunder) {
                    return playRuleBean.twoThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.twoThunderMultiple;
                }
            case 3:
                if (containSpecialThunder) {
                    return playRuleBean.threeThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.threeThunderMultiple;
                }
            case 4:
                if (containSpecialThunder) {
                    return playRuleBean.fourThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.fourThunderMultiple;
                }
            case 5:
                if (containSpecialThunder) {
                    return playRuleBean.fiveThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.fiveThunderMultiple;
                }
            case 6:
                if (containSpecialThunder) {
                    return playRuleBean.sixThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.sixThunderMultiple;
                }
            case 7:
                if (containSpecialThunder) {
                    return playRuleBean.sevenThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.sevenThunderMultiple;
                }
            case 8:
                if (containSpecialThunder) {
                    return playRuleBean.eightThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.eightThunderMultiple;
                }
            case 9:
                if (containSpecialThunder) {
                    return playRuleBean.nineThunderSpecialThunderMultiple;
                } else {
                    return playRuleBean.nineThunderMultiple;
                }
            default:
                return 0;
        }
    }

    //用户定制晥法
    private static double getMultiplezhinan0898(PlayRuleBean playRuleBean, String workedBlessings) {
        List<String> specialThunderList = getSpecialThunderList(playRuleBean.threeThunderSpecialThunder);
        int containCount = 0;
        if (specialThunderList == null || specialThunderList.size() == 0) {
            return playRuleBean.oneThunderMultiple;
        }
        char[] arr = workedBlessings.toCharArray();
        for (char blessingChar : arr) {
            if (specialThunderList.contains(String.valueOf(blessingChar))) {
                containCount = containCount + 1;
            }
        }
        return NumberUtil.subtract(playRuleBean.threeThunderMultiple, containCount);
    }

    /**
     * 获取多镭倍数
     *
     * @param playRuleBean
     * @param workedBlessings
     * @return
     */
    private static double getMoreThunderMultiple(PlayRuleBean playRuleBean, String workedBlessings) {
        List<String> specialThunderList = getSpecialThunderList(playRuleBean.playTypeGroupSpecialThunder);
        boolean oneContainSpecialThunder = false;
        if (specialThunderList != null) {
            for (String specialThunder : specialThunderList) {
                if (workedBlessings.contains(specialThunder)) {
                    oneContainSpecialThunder = true;
                    break;
                }
            }
        }
        if (oneContainSpecialThunder) {
            return playRuleBean.playTypeGroupSpecialThunderMultiple;
        } else {
            return playRuleBean.playTypeGroupOdds;
        }
    }

    /**
     * 获取多镭奖励倍数
     *
     * @param playRuleBean
     * @param workedBlessings
     * @return
     */
    private static String getRewardMoney(PlayRuleBean playRuleBean, AliRedPackageBean aliRedPackageBean, String workedBlessings) {
        double groupRewardMultiple;
        if (aliRedPackageBean == null || aliRedPackageBean.redPackagePickerInfoList == null) {
            return "";
        }
        switch (aliRedPackageBean.redPackagePickerInfoList.size()) {
            case 3:
                switch (workedBlessings.length()) {
                    case 3:
                        groupRewardMultiple = playRuleBean.threeThunderMultiple;
                    case 4:
                        groupRewardMultiple = playRuleBean.fourThunderMultiple;
                    case 5:
                        groupRewardMultiple = playRuleBean.fiveThunderMultiple;
                    case 6:
                        groupRewardMultiple = playRuleBean.sixThunderMultiple;
                    case 7:
                        groupRewardMultiple = playRuleBean.sevenThunderMultiple;
                    default:
                        groupRewardMultiple = 0;
                }
            case 4:
                switch (workedBlessings.length()) {
                    case 4:
                        groupRewardMultiple = playRuleBean.fourThunderMultiple;
                    case 5:
                        groupRewardMultiple = playRuleBean.fiveThunderMultiple;
                    case 6:
                        groupRewardMultiple = playRuleBean.sixThunderMultiple;
                    case 7:
                        groupRewardMultiple = playRuleBean.sevenThunderMultiple;
                    default:
                        groupRewardMultiple = 0;
                }
            case 5:
                switch (workedBlessings.length()) {
                    case 5:
                        groupRewardMultiple = playRuleBean.fiveThunderMultiple;
                    case 6:
                        groupRewardMultiple = playRuleBean.sixThunderMultiple;
                    case 7:
                        groupRewardMultiple = playRuleBean.sevenThunderMultiple;
                    default:
                        groupRewardMultiple = 0;
                }
            case 6:
                switch (workedBlessings.length()) {
                    case 5:
                        groupRewardMultiple = playRuleBean.fiveThunderMultiple;
                    case 6:
                        groupRewardMultiple = playRuleBean.sixThunderMultiple;
                    case 7:
                        groupRewardMultiple = playRuleBean.sevenThunderMultiple;
                    default:
                        groupRewardMultiple = 0;
                }
            default:
                groupRewardMultiple = 0;

        }
        return NumberUtil.multiply(groupRewardMultiple + "", aliRedPackageBean.totalFee);
    }

    private static boolean isHit(MemberBean noDeadInfo, List<AliRedPackagePickerInfo> pickUserList, String workedBlessings, int smallType) {
        String allFen = "";
        boolean isShoot = true;
        for (int i = 0; i < pickUserList.size(); i++) {
            AliRedPackagePickerInfo pickerInfo = pickUserList.get(i);
            if (smallType == 4) {
                allFen = allFen + pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
            } else {
                if (noDeadInfo == null || StringUtils.isEmpty(noDeadInfo.id) ||
                        !noDeadInfo.id.equals(pickerInfo.pickUserId)) {
                    allFen = allFen + pickerInfo.pickMoney.substring(pickerInfo.pickMoney.length() - 1);
                }
            }
        }
        char[] wishingChar = workedBlessings.toCharArray();
        for (char c : wishingChar) {
            if (!allFen.contains(String.valueOf(c))) {
                isShoot = false;
                break;
            }
        }
        return isShoot;
    }

    public static List<String> getSpecialThunderList(String specialThunder) {
        List<String> specialThunderList = new ArrayList<>();
        if (specialThunder == null || "".equals(specialThunder)) {
            return specialThunderList;
        }
        char[] discountChar = specialThunder.toCharArray();
        String workedDiscountStr = "";
        for (int i = 0; i < discountChar.length; i++) {
            if (Character.isDigit(discountChar[i]) || String.valueOf(discountChar[i]).equals("/")) {
                workedDiscountStr = workedDiscountStr + discountChar[i];
            }
        }
        if (!"".equals(workedDiscountStr)) {
            String[] discountArr = workedDiscountStr.split("/");
            specialThunderList = Arrays.asList(discountArr);
        }
        return specialThunderList;
    }

    private static boolean isForbiddenNumber(String forbiddenStr, String blessings) {
        if (StringUtils.isEmpty(forbiddenStr)) {
            return false;
        }
        List<String> forbiddenStrList = Arrays.asList(forbiddenStr.split("/"));
        List<String> workedForbiddenStrList = new ArrayList<>();//对每一个item进行排序，比如32排序后就是23
        for (String str : forbiddenStrList) {
            String sortStr = sort(str);
            workedForbiddenStrList.add(sortStr);
        }
        blessings = sort(blessings);
        for (String forbidden : workedForbiddenStrList) {
            if (blessings.contains(forbidden)) {
                return true;
            }
        }
        return false;
    }

    private static String sort(String str) {
        //利用toCharArray可将字符串转换为char型的数组
        char[] sortStr = str.toCharArray();
        for (int i = 0; i < sortStr.length; i++) {
            for (int j = 0; j < i; j++) {
                if (sortStr[i] < sortStr[j]) {
                    char temp = sortStr[i];
                    sortStr[i] = sortStr[j];
                    sortStr[j] = temp;
                }
            }
        }
        return String.valueOf(sortStr);
    }
}
